package com.thinkcube.edutainment.adapters;

import java.util.List;

import org.androidannotations.annotations.EBean;
import org.androidannotations.annotations.RootContext;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.thinkcube.edutainment.BuyLessonActivity_;
import com.thinkcube.edutainment.R;
import com.thinkcube.edutainment.domain.data.LessonData;

@EBean
public class LessonAdapter extends BaseAdapter implements OnClickListener{

	List<LessonData> lessonData;

	@RootContext
	Context context;

	boolean isCoursePurchase;
	
	
	private  LayoutInflater inflater;

	public boolean isCoursePurchase() {
		return isCoursePurchase;
	}

	public void setCoursePurchase(boolean isCoursePurchase) {
		this.isCoursePurchase = isCoursePurchase;
	}

	
	
	@Override
	public View getView(final int position,  View convertView,
			final ViewGroup parent) {

		/*LessonItem lessonItem=(LessonItem) convertView;
		
		if (convertView == null) {
			
			lessonItem = LessonItem_.build(context);
			ViewHolder viewHolder = new ViewHolder();
			viewHolder.viewItem=lessonItem;   
			lessonItem.setTag(viewHolder);
		} 
		
		    ViewHolder holder=(ViewHolder) lessonItem.getTag();
			//lessonItem = (LessonItem) convertView;
		

		    holder.viewItem.bind(getItem(position),isCoursePurchase);
		return lessonItem;*/
		
		View rowView = convertView;
		
		if(rowView==null)
		{   
			
			inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			
			rowView=inflater.inflate(R.layout.lesson_item, null);
			ViewHolder viewHolder = new ViewHolder();
			
			viewHolder.lessonTitle=(TextView) rowView.findViewById(R.id.lesson_title);
			viewHolder.featured=(TextView) rowView.findViewById(R.id.featured_lesson);
			viewHolder.buy=(Button) rowView.findViewById(R.id.buy_lesson);
			rowView.setTag(viewHolder);
		}
		
		ViewHolder holder = (ViewHolder) rowView.getTag();
		
		holder.lessonTitle.setText(lessonData.get(position).getLessonTitle());
		
		System.out.println("LessonAdapter LESSON TYPE >>>>"+lessonData.get(position).getLessonType());
		
		int type=Integer.valueOf(lessonData.get(position).getLessonType());
		
		System.out.println("LESSON TTILE >>>>>"+lessonData.get(position).getLessonTitle());
		
		holder.buy.setTag(lessonData.get(position));
		holder.buy.setOnClickListener(this);
		
		switch (type) {
		case 0:

			holder.buy.setVisibility(View.INVISIBLE);
			holder.featured.setVisibility(View.INVISIBLE);
			
			break;
			
		case 1:
			
				if(isCoursePurchase){
					
					holder.buy.setVisibility(View.INVISIBLE);
				}
				else if(lessonData.get(position).getLessonPrice().equals("0")){
					
					holder.buy.setVisibility(View.INVISIBLE);
				}
				else{
					
					holder.buy.setVisibility(View.VISIBLE);
				}
				
				holder.featured.setVisibility(View.INVISIBLE);
				
			break;
			
		case 2:
			
			
				System.out.println("this is featured item");
				
				if(lessonData.get(position).getIs_lesson_purchased().equals("true")){
					
					System.out.println("IS LESSON PURCHASE AT LESSONITEM");
					
					holder.buy.setVisibility(View.INVISIBLE);
				}
				
				else{
					
					holder.buy.setVisibility(View.VISIBLE);
				}
				
					holder.featured.setVisibility(View.VISIBLE);
			
			
			break;
			
		default:
			break;
			
		}
		
		return rowView;
	}

	public List<LessonData> getLessonData() {
		return lessonData;
	}

	public void setLessonData(List<LessonData> lessonData) {
		this.lessonData = lessonData;
	}

	@Override
	public int getCount() {
		return lessonData.size();
	}

	@Override
	public LessonData getItem(final int position) {
		return lessonData.get(position);
	}

	@Override
	public long getItemId(final int position) {
		return position;
	}

	class ViewHolder{
		TextView lessonTitle;
		TextView featured;
		Button buy;
	}

	@Override
	public void onClick(View view) {
		
		LessonData lessonData=(LessonData) view.getTag();
		
		System.out.println("BUY >>>>>>"+lessonData.getLessonTitle());
		
		Intent intent = new Intent(context, BuyLessonActivity_.class);
		
		intent.putExtra("lessonObject", lessonData);
		
		//intent.putExtra("paymentData", lessonData);

		((Activity) context).startActivityForResult(intent, 1);
	}
	
	
}
